﻿// =====================================================================
// <copyright file="ExceptionExtensions.cs" author="David R. Guindo">
// 	Copyright (C) 2013. All rights reserved.
// </copyright>
// =====================================================================
// This code is released under the terms of the Apache 2.0 license, 
// http://apache.org/licenses/LICENSE-2.0.html
// =====================================================================

namespace System
{
    /// <summary>
    ///     Exception extensions for show complete error messages
    /// </summary>
    public static class ExceptionExtensions
    {
        /// <summary>
        ///     Gets the complete exception messages.
        /// </summary>
        /// <param name="ex">The ex.</param>
        /// <returns></returns>
        public static string GetStackHierachy(this Exception ex)
        {
            string messages = string.Empty;

            do
            {
                messages += ex.Message + Environment.NewLine + ex.StackTrace + Environment.NewLine;
                ex = ex.InnerException;
            } while (ex != null);

            return messages;
        }

        /// <summary>
        ///     Gets the exception messages.
        /// </summary>
        /// <param name="ex">The ex.</param>
        /// <returns></returns>
        public static string GetMessageHierarchy(this Exception ex)
        {
            string messages = string.Empty;

            do
            {
                messages += ex.Message + Environment.NewLine;
                ex = ex.InnerException;
            } while (ex != null);

            return messages;
        }
    }
}